package com.ncst.mvcweb.service.impl;

import com.ncst.mvcweb.dao.ProductDao;
import com.ncst.mvcweb.dao.ProductPageDao;
import com.ncst.mvcweb.entity.Product;
import com.ncst.mvcweb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductPageDao productPageDao;
    @Override
    public List<Product> findAll() {
        return productDao.findAll();
    }

    @Override
    public Product queryById(Integer id) { return productDao.findById(id).orElse(null); }

    @Override
    public void delete(List<Integer> idList) {
        for (Integer id : idList)
            productDao.deleteById(id);
    }

    @Override
    public Product save(Product product)
    {
        return productDao.save(product);
    }
    @Override
    public void saveAll(List<Product> products) {
        productDao.saveAll(products);
    }

    @Override
    public Page<Product> queryByPage(PageRequest page) {
        return productPageDao.findProductsByOrderById(page);
    }
}
